# coding=utf-8
import uvicorn
from fastapi import FastAPI, Depends, HTTPException,APIRouter
from sqlalchemy.orm import Session
from 学生管理系统.Dao import Students_dao
from 学生管理系统.Mondel.Students_model import Students
from 学生管理系统.Datebase.Datebase import SessionLocal
from 学生管理系统.Query.Students import Students1


app01 = APIRouter()


# 依赖函数：获取数据库会话
def get_db():
    db = SessionLocal()  # 关键：创建实际的会话对象
    try:
        yield db
    finally:
        db.close()  # 确保会话关闭


# 查询所有学生接口
@app01.get("/students/queryall", summary=["查询所有学生信息"])
def get_query(db: Session = Depends(get_db)):
    # 调用dao层sql语句接口
    return Students_dao.get_query(db)


# {student_id}获取学生编号，查询单一学生信息
@app01.get("/students/{student_id}/queryid", summary=["用学生编号查询学生信息"])
def get_query(student_id: int, db: Session = Depends(get_db)):
    stu_info = Students_dao.get_one_query(db, student_id)
    if stu_info is None:
        raise HTTPException(status_code=404,detail='该学生不存在')
    # 调用dao层sql语句接口
    return stu_info


# {student_name}获取学生姓名，查询单一学生信息
@app01.get("/students/{student_name}/queryname", summary=["用学生姓名查询学生信息"])
def get_query(student_name: str, db: Session = Depends(get_db)):
    get_name =  Students_dao.get_name(db, student_name)
    if get_name is None:
        raise HTTPException(status_code=404,detail='该学生不存在')
    # 调用dao层sql语句接口
    return Students_dao.get_name(db, student_name)


# {student_id}/{student_name}/{student_class_name}获取学生编号，学生姓名，班级名称，精准查询学生信息
@app01.get("/students/{student_id}/{student_name}/{student_class_name}/queryone",
         summary=["用学生编号，学生姓名，班级名称，多级联查学生信息"])
def get_query(student_id: int, student_name: str, student_class_name: str, db: Session = Depends(get_db)):
    get_all =Students_dao.get_class_name(db, student_id, student_name, student_class_name)
    if get_all is None:
        raise HTTPException(status_code=404,detail='该学生不存在')
    # 调用dao层sql语句接口
    return Students_dao.get_class_name(db, student_id, student_name, student_class_name)


# 先判断学生是否存在，获取学生编号{student_id}，添加学生接口
@app01.post("/students/{student_id}/add", summary=["添加学生信息"])
def get_query(student_id: int, Student: Students1, db: Session = Depends(get_db)):
    add_stu = Students_dao.get_one_query(db, student_id)
    if add_stu :
        raise HTTPException(status_code=404,detail='该学生已存在')
    student_dao=Students_dao.get_add(db, Student)
    # 调用dao层sql语句接口
    return student_dao


# 先判断学生是否存在，获取学生编号{student_id}，修改学生接口
@app01.put("/students/{student_id}/update",  summary=["修改学生信息"])
def get_query(student_id: int, Student: Students1, db: Session = Depends(get_db)):
    up_stu = Students_dao.get_one_query(db, student_id)
    if up_stu is None :
        raise HTTPException(status_code=404, detail='该学生不存在')    # 调用dao层sql语句接口
    return Students_dao.get_update(db, student_id,Student)


# 先判断学生是否存在，获取学生编号{student_id}，删除学生接口
@app01.get("/students/{student_id}/delete", summary=["删除学生信息"])
def get_query(student_id: int, db: Session = Depends(get_db)):
    del_stu = Students_dao.get_one_query(db, student_id)
    if del_stu is None :
        raise HTTPException(status_code=404, detail='该学生不存在')
    # 调用dao层sql语句接口
    return Students_dao.get_delete(db, student_id)





